package com.Interface.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Interface.dao.ClassInfoMapper;
import com.Interface.dao.ClassMemberAddMapper;
import com.Interface.dao.ClassMemberInfoMapper;
import com.Interface.dao.UserInfoMapper;
import com.Interface.entity.ClassInfo;
import com.Interface.entity.ClassMemberAdd;
import com.Interface.entity.ClassMemberInfo;
import com.Interface.entity.TopicInfo;
import com.Interface.entity.UserInfo;

@Service
public class ClassInfoService {
	@Autowired
	ClassInfoMapper classInfoDao;
	@Autowired
	UserInfoMapper userInfoDao;
	
	@Autowired
	ClassMemberInfoMapper classMemberDao;
	
	@Autowired
	ClassMemberAddMapper  classMemberAddDao;
	
	  public List<Map<String, Object>> selectByKeyWord(Map<String, Object> param){
		  return classInfoDao.selectByKeyWord(param);
		  }
	  public List<Map<String, Object>> getUserJoinClassIdName(int user_id){
		  return classInfoDao.getUserJoinClassIdName(user_id);
	  }
	  public String creatClass(ClassInfo classInfo){
		  
		  int rlt=classInfoDao.insertSelective(classInfo);
		  if(rlt==0){
			  return  "新增班组失败,插入班组表出错";
		  }
		  UserInfo userInfo = new UserInfo();
		  userInfo.setId(classInfo.getCreateUserId());
		  userInfo.setClassId(classInfo.getId());
		  rlt = userInfoDao.updateByCreateTopic(userInfo);
		  if(rlt==0){
			  return  "新增班组失败,更新用户当前班组信息出错";
		  }
		  ClassMemberInfo classMemberInfo = new ClassMemberInfo();
		  classMemberInfo.setClassId(classInfo.getId());
		  classMemberInfo.setUserId(classInfo.getCreateUserId().toString());
		  classMemberInfo.setIsSelf(0);
		  classMemberInfo.setJoinTime(new Date());
		  rlt= classMemberDao.insertSelective(classMemberInfo);
		  if(rlt==0){
			  return  "新增班组失败,插入班组成员失败";
		  }
		  return "";
	  }
	  
	  public int updateByPrimaryKeySelective(ClassInfo record){
		  return classInfoDao.updateByPrimaryKeySelective(record);
	  }
	  public Map<String, Object> getClassInfobyClassId(Map<String, Object> param){
		  return classInfoDao.getClassInfobyClassId(param);
	  }
	  
	  public List<Map<String, Object>> getClassMemberInfo(int class_id){
		  return classInfoDao.getClassMemberInfo(class_id);
	  }
	  
	  public String addMember(Map<String, Object> param){
		  int rlt;
		  int noticeUserId;
		  Map<String, Object> classInfo = classInfoDao.getClassInfobyClassId(param);
		  if(classInfo.get("class_id_use")!=null){
			  return "已经是该班组的成员";
		  }
		  if(param.get("join_type").toString().equals("0")){//主动加入班组
			  if(classInfo.get("is_join_check").toString().equals("1")){ //无需审核之间加入
				  ClassMemberInfo classMemberInfo = new ClassMemberInfo();
				  classMemberInfo.setClassId(Integer.parseInt(param.get("class_id").toString()));
				  classMemberInfo.setUserId(param.get("user_id").toString());
				  classMemberInfo.setIsSelf(1);
				  classMemberInfo.setJoinTime(new Date());
				  rlt= classMemberDao.insertSelective(classMemberInfo);
				  if(rlt==0){
					  return  "新增班组失败,插入班组成员失败";
				  }
				  rlt=classInfoDao.addClassMemberCnt(Integer.parseInt(param.get("class_id").toString()));
				  if(rlt==0){
					  return  "更新班组成员数量出错";
				  }
				  rlt=userInfoDao.addClassCntByUserId(Integer.parseInt(param.get("user_id").toString()));
				  if(rlt==0){
					  return  "更新用户班组数量出错";
				  }
				  
				  
				  return  "0";
			  }
			  noticeUserId = Integer.parseInt(classInfo.get("create_user_id").toString());
		  }else{//班组管理员拉人
			  noticeUserId = Integer.parseInt(param.get("user_id").toString());
		  }
		  ClassMemberAdd memberAdd = new ClassMemberAdd();
		  memberAdd.setClassId(Integer.parseInt(param.get("class_id").toString()));
		  memberAdd.setUserId(Integer.parseInt(param.get("user_id").toString()));
		  memberAdd.setNoticeType(Integer.parseInt(param.get("join_type").toString()));
		  memberAdd.setReason(param.get("reason").toString());
		  memberAdd.setNoticeUserId(noticeUserId);
		  memberAdd.setCreateTime(new Date());
		  rlt = classMemberAddDao.insertSelective(memberAdd);
		  if(rlt==0){
			  return  "新增加入班组通知消息失败";
		  }
		  return "";
	  }
	  public String quitClass(Map<String, Object> param){
		  int rlt=classMemberDao.deleteClassMemberByclassIdUserId(param);
		  if(rlt==1){
			  classInfoDao.reduceClassMemberCnt(Integer.parseInt(param.get("class_id").toString()));
			  userInfoDao.reduceClassCntByUserId(Integer.parseInt(param.get("user_id").toString()));
		  }
		  if(rlt==0){
			  return "不存在该成员或者创建者不能删除";
		  }
		  return "";
	  }
		public List<Map<String, Object>> selectClassPreview(Map<String, Object> param){
			if((int)param.get("type")==0)
				return classInfoDao.selectRecommend(param);
			else if((int)param.get("type")==1)
				return classInfoDao.selectFlow(param);
			else if((int)param.get("type")==2)
				return classInfoDao.selectHot(param);
			else if((int)param.get("type")==3)
				return classInfoDao.selectNew(param);
			else if((int)param.get("type")==4)
				return classInfoDao.selectchoice(param);//精选班组
			return null;
		}
		public ClassInfo selectByPrimaryKey(Integer id){
			return classInfoDao.selectByPrimaryKey(id);
		}
		public List<Map<String, Object>> classhotTopic(int classId){
			return classInfoDao.classhotTopic(classId);
		}
		
		public int getClassCountByKeyWord(Map<String, Object> param){
			return classInfoDao.getClassCountByKeyWord(param);
		}
		
}
